/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.Beasiswa;
import entity.Mahasiswa;
import entity.PengajuanBeasiswa;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.DaftarPendaftarBeasiswa;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author user
 */
public class TambahPengajuanBeasiswa extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PengajuanBeasiswa newpengajuanBeasiswa = new PengajuanBeasiswa();
        Beasiswa beasiswa = new Beasiswa();
        String idBeasiswa = request.getParameter("idBeasiswa");
        beasiswa.setIdBeasiswa(Integer.parseInt(idBeasiswa));
        
        String status = "";
        if (request.getParameter("status") != null) {
            status = request.getParameter("status");
        }

        if (status.equals("submit")) {
            String[] array = new String[9];

                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                //String d = + "-"+c.MONTH+"-"+c.DATE;
                int Month = c.get(Calendar.MONTH) + 1;

                String tanggal = c.get(Calendar.YEAR) + "" + Month + "" + c.get(Calendar.DATE);
                try {
                    PrintWriter out = response.getWriter();

                    // Check that we have a file upload request

                    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

                    // Create a factory for disk-based file items  
                    DiskFileItemFactory factory = new DiskFileItemFactory();

                    // Create a new file upload handler  
                    ServletFileUpload fileUpload = new ServletFileUpload(factory);

                    // Parse the request 
                    List items = fileUpload.parseRequest(request);
                    Iterator ir = items.iterator();
                    int count = 0;
                    
                    DaftarPendaftarBeasiswa daftarPendaftarBeasiswa = new DaftarPendaftarBeasiswa();
                    
                    while (ir.hasNext()) {
                        FileItem item = (FileItem) ir.next();
                        if (item.isFormField()) {
                            //mengambil data input yang selain file
                            out.print(item.getFieldName());
                            array[count] = item.getString();
                            count++;
                        } else {
                            if (item.getFieldName().equals("ktp")) {
                                String nama = tanggal + "_" + item.getName();
                                String location = "D:" + File.separator + "SEMESTER 9" + File.separator + "kemahasiswaanalumni"+ File.separator + "upload";
                                String uploadFile = location + File.separator + nama;
                                File file = new File(uploadFile);
                                item.write(file);

                                newpengajuanBeasiswa.setUploadKtp(nama);
                            } else if (item.getFieldName().equals("listrik")) {
                                String nama = tanggal + "_" + item.getName();
                                String location = "D:" + File.separator + "SEMESTER 9" + File.separator + "kemahasiswaanalumni"+ File.separator + "upload";
                                String uploadFile = location + File.separator + nama;
                                File file = new File(uploadFile);
                                item.write(file);

                                newpengajuanBeasiswa.setUploadListrik(nama);
                            } else if (item.getFieldName().equals("pdam")) {
                                String nama = tanggal + "_" + item.getName();
                                String location = "D:" + File.separator + "SEMESTER 9" + File.separator + "kemahasiswaanalumni"+ File.separator + "upload";
                                String uploadFile = location + File.separator + nama;
                                File file = new File(uploadFile);
                                item.write(file);

                                newpengajuanBeasiswa.setUploadPdam(nama);
                            } else if (item.getFieldName().equals("ipk")) {
                                String nama = tanggal + "_" + item.getName();
                                String location = "D:" + File.separator + "SEMESTER 9" + File.separator + "kemahasiswaanalumni"+ File.separator + "upload";
                                String uploadFile = location + File.separator + nama;
                                File file = new File(uploadFile);
                                item.write(file);

                                newpengajuanBeasiswa.setUploadTranskrip(nama);
                            }
                        }
                    }
                } catch (FileUploadException ex) {
                    log("Error encountered while parsing the request", ex);
                } catch (Exception ex) {
                    log("Error encountered while uploading file", ex);
                }
           
            String idMhs = "5208100003";
            Mahasiswa mhs = new Mahasiswa();
            mhs.setIdUser(idMhs);
            
            newpengajuanBeasiswa.setMahasiswa(mhs);
            newpengajuanBeasiswa.setBeasiswa(beasiswa);
            newpengajuanBeasiswa.setNamaAyah(array[0]);
            newpengajuanBeasiswa.setPekerjaanAyah(array[1]);
            newpengajuanBeasiswa.setNamaIbu(array[2]);
            newpengajuanBeasiswa.setPekerjaanIbu(array[3]);
            newpengajuanBeasiswa.setGajiOrtu(array[4]);
            newpengajuanBeasiswa.setRekListrik(array[6]);
            newpengajuanBeasiswa.setRekPdam(array[7]);
            newpengajuanBeasiswa.setWaktuPengajuan(array[5]);
            newpengajuanBeasiswa.setAlasanPengajuan(array[8]);
            newpengajuanBeasiswa.setStatusPengajuan("0");
            //newBeasiswsetKeteranganBeasiswa(keteranganBeasiswa);
            DaftarPendaftarBeasiswa daftar_pendaftar_beasiswa = new DaftarPendaftarBeasiswa();
            
            daftar_pendaftar_beasiswa.tambahPengajuanBeasiswa(newpengajuanBeasiswa);
            TampilDaftarPendaftarBeasiswa servlet = new TampilDaftarPendaftarBeasiswa();
            servlet.processRequest(request, response);

        } else {
            request.setAttribute("idBeasiswa", idBeasiswa);
            RequestDispatcher dis = request.getRequestDispatcher("pengajuanBeasiswa.jsp");
            dis.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
